package com.linQing.waterWork.service.mongodb.impl;

import com.linQing.waterWork.modle.vo.AnalogValues;
import com.linQing.waterWork.modle.vo.EquipmentStatus;
import com.linQing.waterWork.modle.vo.EquipmentStatusVO;
import com.linQing.waterWork.service.mongodb.EquipmentStatusService;
import com.linQing.waterWork.utils.DateUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Date;

/**
 * @Author wzy
 * @Date 2024/4/30 17:00
 * @Description: TODO
 * @Version 1.0
 */
@Service
@Slf4j
public class EquipmentStatusServiceImpl implements EquipmentStatusService {

    @Resource
    private MongoTemplate mongoTemplate;

    @Override
    public EquipmentStatusVO getEquipmentStatus(String equipmentId) {
        String date = DateUtils.getDate();
        String startDateStr = date + " 23:59:59";
        String endDateStr = date + " 00:00:00";
        Date startDate = new Date(DateUtils.formatD(startDateStr).getTime() - (1000*60*60*24));
        Date endDate = new Date(DateUtils.formatD(endDateStr).getTime() + (1000*60*60*24));
        log.debug("获取从{}到{}的数据", startDate, endDate);

        Query updateTime = Query.query(
                Criteria.where("UpdateTime").gt(startDate).lt(endDate)
                ).with(Sort.by("UpdateTime").descending());
        EquipmentStatus realTime = mongoTemplate.findOne(updateTime, EquipmentStatus.class, equipmentId);
        log.debug("获取到的数据: {}", realTime);
        // 同名属性赋值
        EquipmentStatusVO equipmentStatusVO = new EquipmentStatusVO();
        if(realTime != null) {
            BeanUtils.copyProperties(realTime.getAnalogValues(), equipmentStatusVO);
            log.debug("同名属性复制后: {}", equipmentStatusVO);
        } else {
            return null;
        }
        // 获取昨日最后一条数据
        Date yesterdayStartTime = new Date(startDate.getTime() - (1000*60*60*24));
        Date yesterdayEndTime = new Date(endDate.getTime() - (1000*60*60*24));
        log.debug("昨日数据为{}到{}之间的数据", yesterdayStartTime, yesterdayEndTime);
        Query first = Query.query(
                Criteria.where("UpdateTime").lt(yesterdayEndTime).gt(yesterdayStartTime)
                ).with(Sort.by("UpdateTime").descending());
        EquipmentStatus firstData = mongoTemplate.findOne(first, EquipmentStatus.class, equipmentId);
        log.debug("获取到的昨日最后数据: {}", firstData);
        if(firstData != null) {
            AnalogValues analogValues = firstData.getAnalogValues();
            equipmentStatusVO.setWaterToday(equipmentStatusVO.getCumulativeDischarge()
                    .subtract(analogValues.getCumulativeDischarge()));

            equipmentStatusVO.setElectricityToday(equipmentStatusVO.getAccumulatedElectricity()
                    .subtract(analogValues.getAccumulatedElectricity()));

            if (equipmentStatusVO.getWaterToday() == null || (BigDecimal.ZERO).compareTo(equipmentStatusVO.getWaterToday()) == 0 ) {
                log.debug("当日用水为'0', 手动为千吨能耗赋值为'0.00'");
                equipmentStatusVO.setKilotonEnergyConsumption(new BigDecimal("0.00"));
            } else {
                log.debug("当日用水不为'0', 执行BigDecimal相除方法");
                equipmentStatusVO.setKilotonEnergyConsumption(
                        equipmentStatusVO.getElectricityToday().divide(equipmentStatusVO.getWaterToday(), 2, RoundingMode.HALF_UP)
                                .multiply(new BigDecimal("1000.00"))
                );
            }

        } else {
            return null;
        }
        // 数据组织完毕, 返回实体类
        log.debug("即将返回设备状态信息: {}", equipmentStatusVO);
        return equipmentStatusVO;
    }












}
